<?php

namespace app\common\business;

use app\common\lib\RedisCache;
use app\common\lib\Timer;
use support\exception\BusinessException;
use support\Response;

class BaseBus
{
    /*
     * 错误提示并中断
     */
    public function errorBus($msg = 'error')
    {
        throw new BusinessException($msg);
    }
    /*
     * 静态方法
     * 错误提示并中断
     */
    public static function fail($msg = 'fail')
    {
        throw new BusinessException($msg);
    }

    /**
     * 查询单条数据
     * @param $where
     * @param bool $field
     * @param string $order
     * @return false
     */
    public function getFindData($where, $field = true, $order = 'id desc')
    {
        if (empty($where) && ! is_array($where)) {
            return false;
        }
        $res = $this->model->where($where)->field($field);
        if ($order) {
            $res->order($order);
        }
        return $res->find();
    }

    /**
     * 查询多条数据
     * @param $where
     * @param bool $field
     * @param int $page
     * @param int $num
     * @param string[] $order
     * @return mixed
     */
    public function getDataByWhere($where, $field = true, $page = 0, $num = 10, $order = ['id' => 'desc'])
    {
        $res = $this->model->field($field);
        if (! empty($where) && is_array($where)) {
            $res->where($where);
        }
        if ($page > 0) {
            $res->page($page, $num);
        }
        return $res->order($order)->select();
    }

    /*
     * 修改或获取用户redis缓存
     * @param $token
     * @param string $type
     * @param array $data
     * @return mixed
     */
    public function userCache($token, $type = 'get', $data = [])
    {
        $redisConfig = config('extra.redis.login_token_pre') . $token;
        if ($type == 'set') {
            // 有传递数据
            if ($data) {
                $user = $data;
            } else { // 未传数据
                // 根据token查询用户
                $user = (new \app\common\business\user\UserBus())->getByToken($token);
                if (!$user) {
                    return false;
                }
            }
            if (isset($user['password'])) {
                unset($user['password']);
            }
            // id取模
            if (! isset($user['mo'])) {
                $user['mo'] = $user['id'] % config('extra.status.mo');
            }
            // 存入redis
            RedisCache::set($redisConfig, $user, Timer::loginExpiresTime(), 1);
        }

        $res = RedisCache::get($redisConfig, 1);

        return $res;
    }

    public static function getCurl($url,$data,$headers)
    {
        $data = !empty($data) ? json_encode($data) : '';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
//        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        //捕抓异常
        if (curl_errno($ch)) {
            $result = curl_error($ch);
        }
        curl_close($ch);

        return json_decode($result, true);
    }
}
